import config from './config.js';
import common from "./index.js"

export default {
  request(options) {
    
    let url = options.url;
    if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
    	options.url = config.domain + url;
    }
    
    return new Promise((resolve, reject) => {
      uni.request({
        url: options.url,
        method: options.method || 'GET',
        data: options.data || {},
        header: options.header || {},
        success: res => {
          resolve(res.data)
          // common.showToast(res.data.data.content)
        },
        fail: err => {
          reject(err)
        }
      })
    })
  },
  //封装 DELETE 请求
  del(url, data = {}, header = {}) {
    let options = {
      url: url,
      method: "DELETE",
      data: data,
      header: header
    }
    return this.request(options)
  },
  //封装 PUT 请求
  put(url, data = {}, header = {}) {
    let options = {
      url: url,
      method: "PUT",
      data: data,
      header: header
    }
    return this.request(options)
  },
  //封装 POST 请求
  post(url, data = {}, header = {}) {
    let options = {
      url: url,
      method: "POST",
      data: data,
      header: header
    }
    return this.request(options)
  },
  //封装 GET 请求
  get(url, data = {}, header = {}) {
    let options = {
      url: url,
      method: "GET",
      data: data,
      header: header
    }
    return this.request(options)
  }
}